﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NorthwindModel;
using NorthwindDAL.Repositories;

namespace NorthwindDAL
{
    public class UnitOfWork : IUnitOfWork, IDisposable
    {
        private NorthwindContext context;
        private CustomerRepository customerRepository;
        private bool dsiposed = false;

        public UnitOfWork()
        {
            context = new NorthwindContext();
        }

        public CustomerRepository CustomerRepository
        {
            get
            {
                if (this.customerRepository == null)
                {
                    this.customerRepository = new CustomerRepository(context);
                }
                return customerRepository;
            }
        }

        public int Save()
        {
            return context.SaveChanges();
        }

        protected virtual void Dispose(bool dispsing)
        {
            if (!this.dsiposed)
            {
                if (dispsing)
                {
                    customerRepository.Dispose();
                    context.Dispose();
                }
                this.dsiposed = true;
            }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}
